package com.kingdee.bos.service.formula.api;

import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.kscript.KScriptException;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemCollection;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.bos.metadata.entity.SelectorItemInfo;
import com.kingdee.eas.basedata.master.account.AccountViewFactory;
import com.kingdee.eas.basedata.master.account.AccountViewInfo;
import com.kingdee.eas.basedata.master.auxacct.GeneralAsstActTypeInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.cp.bc.BizAccountBillFactory;
import com.kingdee.eas.cp.bc.BizAccountBillInfo;
import com.kingdee.eas.cp.bc.BizAccountOutBillFactory;
import com.kingdee.eas.cp.bc.BizAccountOutBillInfo;
import com.kingdee.eas.cp.bc.DailyLoanBill;
import com.kingdee.eas.cp.bc.DailyLoanBillAccountEntry;
import com.kingdee.eas.cp.bc.DailyLoanBillAccountEntryFactory;
import com.kingdee.eas.cp.bc.DailyLoanBillFactory;
import com.kingdee.eas.cp.bc.DailyLoanBillInfo;
import com.kingdee.eas.cp.bc.TravelAccountBillFactory;
import com.kingdee.eas.cp.bc.TravelAccountBillInfo;
import com.kingdee.eas.custom.basedata.BudgetAccoCollection;
import com.kingdee.eas.custom.basedata.BudgetAccoFactory;
import com.kingdee.eas.custom.basedata.BudgetAccoInfo;
import com.kingdee.eas.custom.basedata.BudgetAssiCollection;
import com.kingdee.eas.custom.basedata.BudgetAssiFactory;
import com.kingdee.eas.custom.basedata.BudgetAssiInfo;

public class CustomBTPFunction implements IFormulaFunctions {

	//用于存放公式的变量
	private static Vector funcInfos;
	
	static
	{
		//加载类时定义自定义公式
		funcInfos = new Vector();
		//FuncInfo()的第一个参数是公式名，第二个参数是公式所在的分类（可随便取名），第三个参数是公式的描述信息（在BOTP公式平台显示）
		funcInfos.add(new FuncInfo("__BOTgetAsstActTypeByProject", "费用报销扩展开发", 
				"根据费用项目id取核算项目__BOTgetAsstActTypeByProject(String 费用项目id,int 第几核算项目)"));
		funcInfos.add(new FuncInfo("__BOTgetAccountOnCampus", "费用报销扩展开发", 
		"根据费用项目id取会计项目__BOTgetAccountOnCampus(String 费用项目id,String 院区)"));
		funcInfos.add(new FuncInfo("__BOTgetCollectionNum", "费用报销扩展开发", 
		"根据费用报销单ID取收款人条数__BOTgetCollectionNum(String 费用报销单ID,int 费用报销单1:借款单2:费用报销单3:对公费用报销单4:差旅费报销单)"));

	}
	
	/**
	* 运行公式时调用此方法，公式的具体逻辑就写在这里
	* @author Yang Yihao. Just call me House.
	* @date since 2007-9-6
	* @Email: dejavu.house@gmail.com
	* @param func 公式名称
	* @param paramList 公式的参数
	* @return 公式结果
	* @throws KScriptException
	*/
	public Object evalFunction(String func, List paramList) throws KScriptException {
		try{
			 Context ctx = (Context)paramList.get(0);
				//判断用户是选了哪个公式
				if(func != null && func.equals("__BOTgetAsstActTypeByProject"))
				{
					if ((paramList == null) || (paramList.size() < 3))
						throw new InvokeFunctionException(func, 2);
					if (paramList.size() > 3)
						throw new InvokeFunctionException(func, 1);
					if (paramList.get(1) == null)
						return null;
					int num = 0;
					if (paramList.get(2) != null) {
						num = Integer.valueOf(paramList.get(2).toString());
					}
					
					EntityViewInfo view = new EntityViewInfo();
					FilterInfo filter = new FilterInfo();
					filter.getFilterItems().add(new FilterItemInfo("budgetItem.id", paramList.get(1)));
					view.setFilter(filter);
					SelectorItemCollection sic =new SelectorItemCollection();
					sic.add(new SelectorItemInfo("*"));
					sic.add(new SelectorItemInfo("checkItem1.*"));
					sic.add(new SelectorItemInfo("checkItem2.*"));
					sic.add(new SelectorItemInfo("checkItem3.*"));
					view.setSelector(sic);
					BudgetAssiCollection bac = BudgetAssiFactory.getLocalInstance(ctx).getBudgetAssiCollection(view);
					if(bac.size()>0){
						BudgetAssiInfo bai = bac.get(0);
						GeneralAsstActTypeInfo info=null;
						switch (num) {
						case 1:
							info=bai.getCheckItem1();
							break;
						case 2:
							info=bai.getCheckItem2();
							break;
						case 3:
							info=bai.getCheckItem3();
							break;
						default:
							break;
						}
						return info;
					}else{
						return null;
					}
				}else if(func != null && func.equals("__BOTgetAccountOnCampus")){
					if ((paramList == null) || (paramList.size() < 3))
						throw new InvokeFunctionException(func, 2);
					if (paramList.size() > 3)
						throw new InvokeFunctionException(func, 1);
					if (paramList.get(1) == null)
						return null;
					
					EntityViewInfo view = new EntityViewInfo();
					FilterInfo filter = new FilterInfo();
					filter.getFilterItems().add(new FilterItemInfo("budgetItem.id", paramList.get(1)));
					filter.getFilterItems().add(new FilterItemInfo("campus", paramList.get(2)));
					view.setFilter(filter);
					SelectorItemCollection sic =new SelectorItemCollection();
					sic.add(new SelectorItemInfo("*"));
					sic.add(new SelectorItemInfo("accounSubject.*"));
					view.setSelector(sic);
					BudgetAccoCollection bac = BudgetAccoFactory.getLocalInstance(ctx).getBudgetAccoCollection(view);
					if(bac.size()>0){
						BudgetAccoInfo bai = bac.get(0);
						return bai.getAccounSubject();
					}else{
						return null;
					}
				}else if(func != null && func.equals("__BOTgetCollectionNum")){
//					if ((paramList == null) || (paramList.size() < 3))
//						throw new InvokeFunctionException(func, 2);
//					if (paramList.size() > 2)
//						throw new InvokeFunctionException(func, 1);
//					if (paramList.get(1) == null)
//						return null;
					int num = 0;
					if (paramList.get(2) != null) {
						num = Integer.valueOf(paramList.get(2).toString());
					}
					
					try {
						int number=0;
						switch (num) {
						case 1:
							DailyLoanBillInfo dailyLoanBillInfo = DailyLoanBillFactory.getLocalInstance(ctx).getDailyLoanBillInfo(new ObjectUuidPK(paramList.get(1).toString()));
							number=dailyLoanBillInfo.getCollectionEntries().size();
							break;
						case 2:
							BizAccountBillInfo bizAccountBillInfo = BizAccountBillFactory.getLocalInstance(ctx).getBizAccountBillInfo(new ObjectUuidPK(paramList.get(1).toString()));				
							number=bizAccountBillInfo.getCollectionEntries().size();
							break;
						case 3:
							BizAccountOutBillInfo bizAccountOutBillInfo = BizAccountOutBillFactory.getLocalInstance(ctx).getBizAccountOutBillInfo(new ObjectUuidPK(paramList.get(1).toString()));
							number=bizAccountOutBillInfo.getCollectionEntries().size();
							break;
						case 4:
							TravelAccountBillInfo travelAccountBillInfo = TravelAccountBillFactory.getLocalInstance(ctx).getTravelAccountBillInfo(new ObjectUuidPK(paramList.get(1).toString()));
							number=travelAccountBillInfo.getCollectionEntries().size();
							break;
						default:
							break;
						}
						return number;
					} catch (EASBizException e) {
						e.printStackTrace();
					}
					return null;
				}
		} catch (InvokeFunctionException e) {
			e.printStackTrace();
		} catch (BOSException e) {
			e.printStackTrace();
		} 
		
		return null;
	}

	public boolean existFunction(String s) {
		if(s == null)
			return false;
		for(int i = 0; i < funcInfos.size(); i++)
			if(s.equals(((FuncInfo)funcInfos.get(i)).funcName))
				return true;
		return false;
	}

	/**
	* 运行时BOTP公式平台加载自定义公式时，就是从该返回值里取公式的名称的
	* @author Yang Yihao. Just call me House.
	* @date since 2007-9-6
	* @Email: dejavu.house@gmail.com
	* @return 所有自定义公式的名称
	*/
	public String[] getAllFuncNames() {
		String as[] = new String[funcInfos.size()];
		for(int i = 0; i < funcInfos.size(); i++)
			as[i] = ((FuncInfo)funcInfos.get(i)).funcName;
		return as;
	}

	/**
	* 运行时BOTP公式平台加载自定义公式时，就是从该返回值里取公式的分类的（看运
	行时的界面就很好理解）
	* @author Yang Yihao. Just call me House.
	* @date since 2007-9-6
	* @Email: dejavu.house@gmail.com
	* @param s
	* @return
	*/
	public String getFuncCategory(String s) {
		if(s == null)
			return null;
		for(int i = 0; i < funcInfos.size(); i++)
			if(s.equals(((FuncInfo)funcInfos.get(i)).funcName))
				return ((FuncInfo)funcInfos.get(i)).funcCategory;
		return null;
	}

	/**
	* 获取公式的描述
	* @author Yang Yihao. Just call me House.
	* @date since 2007-9-6
	* @Email: dejavu.house@gmail.com
	* @param s
	* @return
	*/
	public String getFuncDesc(String s) {
		if(s == null)
			return null;
		for(int i = 0; i < funcInfos.size(); i++)
			if(s.equals(((FuncInfo)funcInfos.get(i)).funcName))
				return ((FuncInfo)funcInfos.get(i)).funcDesc;
		return null;
	}

}
